package com.ctshk.rpc.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.service.support.BaseService;
import com.ctshk.rpc.tour.entity.SeriesTrip;
import com.ctshk.rpc.tour.entity.SeriesTripScheduleItineraryDay;
import com.ctshk.rpc.tour.mapper.SeriesTripMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripScheduleItineraryDayMapper;
import com.ctshk.rpc.tour.req.SeriesTripScheduleItineraryDayReq;
import com.ctshk.rpc.tour.req.es.TripCacheUpdateReq;
import com.ctshk.rpc.tour.service.ISeriesTripAppService;
import com.ctshk.rpc.tour.service.ISeriesTripScheduleItineraryDayService;
import ma.glasnost.orika.MapperFactory;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * <p>
 * 行程每日介绍 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-13
 */
@DubboService
public class SeriesTripScheduleItineraryDayServiceImpl extends ServiceImpl<SeriesTripScheduleItineraryDayMapper, SeriesTripScheduleItineraryDay> implements ISeriesTripScheduleItineraryDayService, BaseService {

    /**
     * 数据转换
     */
    @Autowired
    private MapperFactory mapperFactory;

    /**
     * 行程主表服务
     */
    @Autowired
    private SeriesTripMapper seriesTripMapper;

    @Autowired
    private ISeriesTripAppService iSeriesTripAppService;


    @Override
    public void itineraryInformation(SeriesTripScheduleItineraryDayReq seriesTripScheduleItineraryDayReq, Long userId) {

        if (seriesTripScheduleItineraryDayReq.getItineraryType() == 1) {
            //新增行程基本资料
            List<SeriesTripScheduleItineraryDay> list = mapperFactory.getMapperFacade().mapAsList(seriesTripScheduleItineraryDayReq.getList(), SeriesTripScheduleItineraryDay.class);
            fillCreateEntityUserId(list, userId);
            baseMapper.delete(new QueryWrapper<SeriesTripScheduleItineraryDay>().lambda().eq(SeriesTripScheduleItineraryDay::getTripId, seriesTripScheduleItineraryDayReq.getId()));
            list.forEach(seriesTripScheduleItineraryDay -> seriesTripScheduleItineraryDay.setTripId(seriesTripScheduleItineraryDayReq.getId()));
            super.saveBatch(list);
        }
        SeriesTrip seriesTrip = newUpdateEntityUserId(SeriesTrip.class, null, userId);
        if (seriesTripScheduleItineraryDayReq.getItineraryType() == 2) {
            seriesTrip.setSimpleItinerary(seriesTripScheduleItineraryDayReq.getSimpleJourneyReq().getSimpleItinerary());
        }
        seriesTrip.setId(seriesTripScheduleItineraryDayReq.getId());
        seriesTrip.setItineraryType(seriesTripScheduleItineraryDayReq.getItineraryType());
        seriesTripMapper.updateById(seriesTrip);

        // 更新缓存
        CompletableFuture.runAsync(() -> {
            iSeriesTripAppService.updateTripCache(new TripCacheUpdateReq().setIds(Arrays.asList(seriesTripScheduleItineraryDayReq.getId())));
        });
    }
}
